package org.mch.swingm.ext;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import org.mch.swingm.core.MFileChooser;
import org.mch.swingm.core.MLabel;
import org.mch.swingm.core.MTextField;
import org.mch.swingm.itf.IMExtComponent;
import org.mch.swingm.obj.MItem;

public class METextChooser extends JPanel implements IMExtComponent {
	private static final long serialVersionUID = -8975642161941253889L;

	/** 选择【文件夹】 */
	public static final int SELECT_FOLDER = 0;
	/** 选择【文件】 */
	public static final int SELECT_FILE = 1;

	private MTextField textFile;
	private JButton btnBrowser;
	private MFileChooser fileChooser;
	private MLabel label;
	private int dialogType = SELECT_FILE;
	private MItem item;

	public METextChooser() {
		super();
		initGUI();
	}

	public METextChooser(int dialogType) {
		super();
		this.dialogType = dialogType;
		initGUI();
	}

	public METextChooser(String name) {
		super();
		this.item = new MItem(name);
		initGUI();
	}

	public METextChooser(String name, int dialogType) {
		super();
		this.item = new MItem(name);
		this.dialogType = dialogType;
		initGUI();
	}

	public METextChooser(MItem item) {
		super();
		this.item = item;
		initGUI();
	}

	public METextChooser(MItem item, int dialogType) {
		super();
		this.item = item;
		this.dialogType = dialogType;
		initGUI();
	}

	private void initGUI() {
		setBorder(new LineBorder(Color.LIGHT_GRAY));
		setLayout(new BorderLayout(0, 0));
		add(getComponent(), BorderLayout.CENTER);
		add(getBtnBrowser(), BorderLayout.EAST);
		add(getLabel(), BorderLayout.WEST);
	}

	public MTextField getComponent() {
		if (textFile == null) {
			textFile = new MTextField();
			textFile.setFont(new Font("宋体", Font.PLAIN, 14));
			textFile.setBorder(null);
			textFile.setColumns(10);
		}
		return textFile;
	}

	public JButton getBtnBrowser() {
		if (btnBrowser == null) {
			btnBrowser = new JButton("");
			btnBrowser.addMouseListener(new BtnBrowserClick());
			btnBrowser.setIcon(new ImageIcon(METextChooser.class.getResource("/org/jdesktop/swingx/color/mag.png")));
			btnBrowser.setFont(new Font("宋体", Font.PLAIN, 12));
		}
		return btnBrowser;
	}

	@Override
	public void setValue(Object s) {
	}

	public String getValue() {
		return getComponent().getText();
	}

	public void setText(String path) {
		getComponent().setText(path);
	}

	public MFileChooser getFileChooser() {
		/*
		 * 
		 * JFileChooser.FILES_ONLY JFileChooser.DIRECTORIES_ONLY JFileChooser.FILES_AND_DIRECTORIES
		 */
		if (fileChooser == null) {
			fileChooser = new MFileChooser();
			fileChooser.setPreferredSize(700, 400);
			switch (getDialogType()) {
				case SELECT_FILE: {
					// 选择文件
					fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
					break;
				}
				case SELECT_FOLDER: {// 选择文件夹
					fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					break;
				}
			}
		}
		return fileChooser;
	}

	public MLabel getLabel() {
		if (label == null) {
			label = new MLabel(getItem().getName());
			label.setBorder(new LineBorder(SystemColor.controlHighlight));
			label.setFont(new Font("宋体", Font.PLAIN, 13));
			label.setToolTipText(getItem().getHint());
		}
		return label;
	}

	public int getDialogType() {
		return dialogType;
	}

	public MItem getItem() {
		return item;
	}

	public String getKey() {
		return getItem().getKey();
	}

	class BtnBrowserClick extends MouseAdapter {
		private Component comp = SwingUtilities.getRoot(METextChooser.this);

		@Override
		public void mouseClicked(MouseEvent e) {
			getFileChooser().setSelectedFile(new File(getValue()));
			int option = -1;
			switch (getDialogType()) {
				case MFileChooser.OPEN_DIALOG: {
					option = getFileChooser().showOpenDialog(comp);
					break;
				}
				case MFileChooser.SAVE_DIALOG: {
					option = getFileChooser().showSaveDialog(comp);
					break;
				}
			}
			if (option == JFileChooser.APPROVE_OPTION) {
				File file = getFileChooser().getSelectedFile();
				setText(file == null ? "" : file.getPath());
			}
		}
	}

}
